﻿using AspNetCoreRateLimit;
using Lazy.Captcha.Core;
using Masuit.Tools.Hardware;
using Microsoft.AspNetCore.Authorization;
using Microsoft.IdentityModel.Tokens;
using My.Admin.Util;
using My.Core.DynamicWebApi.Attributes;
using My.Framework;
using NewLife.Caching;
using SqlSugar;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Yitter.IdGenerator;

namespace My.Admin.Dynamic
{
	[Service("SysServerController")]
	public class SysServerController
	{
		/// <summary>
		/// 获取服务器配置信息
		/// </summary>
		/// <returns></returns>
		[Authorize]
		public dynamic GetServerBase()
		{
			return new
			{
				HostName = Environment.MachineName, // 主机名称
				SystemOs = RuntimeInformation.OSDescription, // 操作系统
				OsArchitecture = Environment.OSVersion.Platform.ToString() + " " + RuntimeInformation.OSArchitecture.ToString(), // 系统架构
				ProcessorCount = Environment.ProcessorCount + " 核", // CPU核心数
				SysRunTime = SystemInfo.BootTime().ToString(), // 系统运行时间
				RemoteIp ="", // 外网地址
				LocalIp = SystemInfo.GetLocalIPs().ElementAt(0).Address.ToString(), // 本地地址
				FrameworkDescription = RuntimeInformation.FrameworkDescription, // NET框架
				Environment = "",
				Wwwroot = "", // 网站根目录
				Stage = "", // 是否Stage环境
			};
		}

		/// <summary>
		/// 获取服务器使用信息
		/// </summary>
		/// <returns></returns>
		[Authorize]
		public dynamic GetServerUsed()
		{
			var programStartTime = Process.GetCurrentProcess().StartTime;
			var totalMilliseconds = (DateTime.Now - programStartTime).TotalMilliseconds.ToString();
			var ts = totalMilliseconds.Contains('.') ? totalMilliseconds.Split('.')[0] : totalMilliseconds;
			var programRunTime = (DateTime.Now - Process.GetCurrentProcess().StartTime).TotalMinutes.ToString("f2");
			
			var memoryMetrics = RamInfo.Local;
			var UsedRam = memoryMetrics.PhysicalMemory - memoryMetrics.MemoryAvailable;
			var FreeRam = memoryMetrics.MemoryAvailable;
			var TotalRam = memoryMetrics.PhysicalMemory;
			var RamRate = memoryMetrics.MemoryUsage.ToString("f2");
			var CpuRate = SystemInfo.CpuLoad.ToString("f2");
			return new
			{
				FreeRam, // 空闲内存
				UsedRam, // 已用内存
				TotalRam, // 总内存
				RamRate, // 内存使用率
				CpuRate, // Cpu使用率
				StartTime = programStartTime.ToString("yyyy-MM-dd HH:mm:ss"), // 服务启动时间
				RunTime = programRunTime, // 服务运行时间
			};
		}

		/// <summary>
		/// 获取服务器磁盘信息
		/// </summary>
		/// <returns></returns>
		[Authorize]
		public dynamic GetServerDisk()
		{
			var l = DiskInfo.Locals;
			return l;
		}

		/// <summary>
		/// 获取框架主要程序集
		/// </summary>
		/// <returns></returns>
		[Authorize]
		public dynamic GetAssemblyList()
		{
			var furionAssembly = typeof(App).Assembly.GetName();
			var sqlSugarAssembly = typeof(ISqlSugarClient).Assembly.GetName();
			var yitIdAssembly = typeof(YitIdHelper).Assembly.GetName();
			var redisAssembly = typeof(Redis).Assembly.GetName();
			//var jsonAssembly = typeof(NewtonsoftJsonMvcCoreBuilderExtensions).Assembly.GetName();
			//var excelAssembly = typeof(IExcelImporter).Assembly.GetName();
			//var pdfAssembly = typeof(IPdfExporter).Assembly.GetName();
			var captchaAssembly = typeof(ICaptcha).Assembly.GetName();
			//var ossAssembly = typeof(IOSSServiceFactory).Assembly.GetName();
			//var parserAssembly = typeof(Parser).Assembly.GetName();
			//var nestAssembly = typeof(IElasticClient).Assembly.GetName();
			var limitAssembly = typeof(IpRateLimitMiddleware).Assembly.GetName();
			//var htmlParserAssembly = typeof(HtmlParser).Assembly.GetName();
			//var fluentEmailAssembly = typeof(IFluentEmail).Assembly.GetName();

			return new[]
			{
				new { furionAssembly.Name, furionAssembly.Version },
				new { sqlSugarAssembly.Name, sqlSugarAssembly.Version },
				new { yitIdAssembly.Name, yitIdAssembly.Version },
				new { redisAssembly.Name, redisAssembly.Version },
				//new { jsonAssembly.Name, jsonAssembly.Version },
				//new { excelAssembly.Name, excelAssembly.Version },
				//new { pdfAssembly.Name, pdfAssembly.Version },
				new { captchaAssembly.Name, captchaAssembly.Version },
				//new { ossAssembly.Name, ossAssembly.Version },
				//new { parserAssembly.Name, parserAssembly.Version },
				//new { nestAssembly.Name, nestAssembly.Version },
				new { limitAssembly.Name, limitAssembly.Version },
				//new { htmlParserAssembly.Name, htmlParserAssembly.Version },
				//new { fluentEmailAssembly.Name, fluentEmailAssembly.Version },
			};
		}
	}
}
